package com.eco.fanliapp.controller.app;


import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.service.ISyUserFootprintService;
import com.eco.fanliapp.utils.DateFormatUtils;
import com.eco.fanliapp.utils.DateUtils;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 足迹表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/app/user")
public class SyUserFootprintController extends BaseController {

    @Autowired
    private ISyUserFootprintService iSyUserFootprintService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 插入足迹
     * @param itemId
     * @return
     */
    @GetMapping("/footprint/toDo")
    public ServerResponse footprintToDo(Long itemId) {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        //存redis
        //long minute = new Date().getMinutes();
        String time = DateFormatUtils.getNowDateMin(new Date());
        if(redisTemplate.hasKey("footprintToDo_"+time)){
            Map<String,Set<Long>> map = (Map<String, Set<Long>>)redisTemplate.opsForValue().get("footprintToDo_"+time);
            if(map.containsKey(u.getUserId().toString())){
                Set<Long> list = map.get(u.getUserId().toString());
                list.add(itemId);
                map.put(u.getUserId().toString(),list);
                redisTemplate.opsForValue().set("footprintToDo_"+time,map,60 * 60 * 2 , TimeUnit.SECONDS);
            }else {
                Set<Long> list = new HashSet<>();
                list.add(itemId);
                map.put(u.getUserId().toString(),list);
                redisTemplate.opsForValue().set("footprintToDo_"+time,map,60 * 60 * 2, TimeUnit.SECONDS);
            }

        }else {
            Map<String,Set<Long>> map = new HashMap<>();
            Set<Long> list = new HashSet<>();
            list.add(itemId);
            map.put(u.getUserId().toString(),list);
            redisTemplate.opsForValue().set("footprintToDo_"+time,map,60 * 60 * 2 , TimeUnit.SECONDS);
        }
        return ServerResponse.createBySuccessMessage("足迹记录成功");
//        return iSyUserFootprintService.footprintToDo(u.getUserId(),itemId);
    }

    /**
     * 删除足迹
     * @param itemId
     * @return
     */
    @GetMapping("/footprint/remove")
    public ServerResponse footprintRemove(Long itemId) {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserFootprintService.footprintRemove(u.getUserId(),itemId);
    }

    /**
     * 足迹列表
     * @return
     */
    @GetMapping("/footprint/list")
    public ServerResponse footprintList(Long pageNum,Long pageSize) {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserFootprintService.footprintList(u.getUserId(),pageNum,pageSize);
    }

    /**
     * 找相似
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/similar/list")
    public ServerResponse similarList(String keyword, Long pageNum,Long pageSize) {
        AppUser u = getAppUser();
        if(null == u) {
            return ServerResponse.createByErrorCodeMessage(301,"Token不正确");
        }
        return iSyUserFootprintService.similarList(u.getUserId(),keyword,pageNum,pageSize);
    }

    /**
     * 商品点击数
     * @param
     * @return
     */
//    @GetMapping("/appGoods/hitCount")
//    public ServerResponse appGoodsHitCount(Long itemId) {
//        return iSyUserFootprintService.appGoodsHitCount(itemId);
//    }

}
